package com.hexacta.hrs.domain.mentoring;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.vectrics.common.db.AbstractRecordObject;
import org.vectrics.common.util.DateUtil;
import org.vectrics.employee.Employee;

/**
 * Class that represents a meeting
 * 
 * @author vschimpf
 */
public class Meeting extends AbstractRecordObject {

    // Colores de las filas seg�n el valor de la propiedad delayedDays

    public static String TEXT_GREEN = "#A7D34B";

    public static String TEXT_YELLOW = "#FFA200";

    public static String TEXT_RED = "#FF0000";

    /** Employee. */
    private Employee employee;

    /** Mentor Employee. */
    private Employee mentor;

    private Set<Theme> noteThemes = new HashSet<Theme>();

    private Set<Action> actionsToDo = new HashSet<Action>();

    private Date date;

    private MeetingStatus status;

    private Boolean checkedByHR;

    private Meeting previousMeeting;

    private Integer delayedDays;

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(final Employee employee) {
        this.employee = employee;
    }

    public Employee getMentor() {
        return mentor;
    }

    public void setMentor(final Employee mentor) {
        this.mentor = mentor;
    }

    public Set<Theme> getNoteThemes() {
        return noteThemes;
    }

    public void setNoteThemes(final Set<Theme> noteThemes) {
        this.noteThemes = noteThemes;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(final Date date) {
        // Note: compare same day, month, year, hour and minutes (without take
        // into account seconds and milliseconds)
        this.date = DateUtil.roundToMinutes(date);
    }

    public MeetingStatus getStatus() {
        return status;
    }

    public void setStatus(final MeetingStatus status) {
        this.status = status;
    }

    public Set<Action> getActionsToDo() {
        return actionsToDo;
    }

    public void setActionsToDo(final Set<Action> actionsToDo) {
        this.actionsToDo = actionsToDo;
    }

    public Boolean getCheckedByHR() {
        return checkedByHR;
    }

    public void setCheckedByHR(final Boolean checkedByHR) {
        this.checkedByHR = checkedByHR;
    }

    public Meeting getPreviousMeeting() {
        return previousMeeting;
    }

    public void setPreviousMeeting(final Meeting previousMeeting) {
        this.previousMeeting = previousMeeting;
    }

    /**
     * A partir de la lista de feriados y el maximo periodo entre meetings,
     * calcula los d�as de demora entre s� misma y la meeting previa.
     * 
     * @param holidays
     * @param maxPeriodBetweenMeetings
     */
    public void calculateDelayedDays(final List<Date> holidays, final Integer maxPeriodBetweenMeetings) {
        if (getPreviousMeeting() != null) {
            int daysBetween = DateUtil.getWorkingDaysBetween(getPreviousMeeting().getDate(), getDate(), holidays) - 1;
            delayedDays = daysBetween <= maxPeriodBetweenMeetings ? 0 : daysBetween - maxPeriodBetweenMeetings;
        } else {
            delayedDays = 0;
        }
    }

    public Integer getDelayedDays() {
        return delayedDays;
    }

    /**
     * Dependiendo los d�as de demora devuelve el color de la fila
     * 
     * @return
     */
    public String getTextColor() {
        if (getDelayedDays() != null && getDelayedDays() > 0) {
            if (getDelayedDays() > 15)
                return TEXT_RED;
            else
                return TEXT_YELLOW;
        } else
            return TEXT_GREEN;
    }

    public Boolean getOnlyCurrentRelationships() {
        return getMentor().getId().equals(getEmployee().getMentor().getId());
    }

    /**
     * Este m�todo crea una nueva instancia y copia todas las propiedades
     * excepto la PreviousMeeting
     */
    public Meeting copyExceptPreviousMeeting() {
        Meeting copyMeeting = new Meeting();
        copyMeeting.setId(getId());
        copyMeeting.setCheckedByHR(getCheckedByHR());
        copyMeeting.setDate(getDate());
        copyMeeting.setEmployee(getEmployee());
        copyMeeting.setMentor(getMentor());
        copyMeeting.setStatus(getStatus());

        for (Theme theme : getNoteThemes()) {
            Theme themeDto = theme.copy();
            copyMeeting.getNoteThemes().add(themeDto);
        }

        for (Action action : getActionsToDo()) {
            Action actionDto = action.copy();
            copyMeeting.getActionsToDo().add(actionDto);
        }

        return copyMeeting;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o)
            return true;
        if (o == null)
            return false;
        if (!(o instanceof Meeting))
            return false;
        final Meeting other = (Meeting) o;

        EqualsBuilder eb = new EqualsBuilder();
        eb.append(getEmployee(), other.getEmployee());
        eb.append(getMentor(), other.getMentor());
        eb.append(getDate(), other.getDate());
        eb.append(getStatus(), other.getStatus());
        eb.append(getCheckedByHR(), other.getCheckedByHR());
        eb.append(getNoteThemes().toArray(), other.getNoteThemes().toArray());
        eb.append(getActionsToDo().toArray(), other.getActionsToDo().toArray());
        if (MeetingStatus.PERFORMED.equals(getStatus())) {
            eb.append(getId(), other.getId());
        }

        return eb.isEquals();
    }

    @Override
    public int hashCode() {
        HashCodeBuilder hcb = new HashCodeBuilder();
        hcb.append(getEmployee());
        hcb.append(getMentor());
        hcb.append(getCheckedByHR());
        hcb.append(getDate());
        hcb.append(getStatus());
        hcb.append(getNoteThemes().toArray());
        hcb.append(getActionsToDo().toArray());
        if (MeetingStatus.PERFORMED.equals(getStatus())) {
            hcb.append(getId());
        }
        return hcb.toHashCode();
    }

    @Override
    public String toString() {

        return "Employee: " + getEmployee().getPerson().getFirstName().toString() + ", Mentor: "
                + getMentor().getPerson().getFirstName().toString() + ", Date: "
                + DateUtil.getFechaFormateada(getDate());
    }

}